package com.yangguang.huahui.mvp.presenter;

import com.google.gson.Gson;
import com.yangguang.huahui.entity.HttpResult;
import com.yangguang.huahui.entity.Upload;
import com.yangguang.huahui.mvp.model.SupplyModel;
import com.yangguang.huahui.mvp.model.UploadModel;
import com.yangguang.huahui.mvp.view.PublishView;
import com.yangguang.huahui.rx.RxManager;
import com.yangguang.huahui.rx.RxSubscriber;
import com.yangguang.huahui.utils.LogUtil;

import java.io.IOException;
import java.util.List;

import okhttp3.ResponseBody;

/**
 * Created by Administrator on 2017/3/27.
 */

public class PublishSupplyPresent extends BasePresenter<PublishView>{
    SupplyModel model;

    public PublishSupplyPresent() {
        model = new SupplyModel();
    }
    public void publish(String uid, String goods_type, String variety, String goods_specifications, String goods_title,
                        String source_of_supply_id, String price, String whether_the_spot, String time_to_market, String expire_date,
                        String service_method, String carousel, String contacts, String phone, String supply_mark, String remarks){
        mSubscription = RxManager.getInstance().doSubscribe(model.publish(uid, goods_type, variety, goods_specifications, goods_title,
                source_of_supply_id, price, whether_the_spot, time_to_market, expire_date, service_method, carousel, contacts, phone, supply_mark, remarks), new RxSubscriber<HttpResult>(false) {
            @Override
            protected void _onNext(HttpResult httpResult) {
                mView.onSuccess(httpResult);
            }

            @Override
            protected void _onError() {
                mView.onError();
            }
        });
    }
    public void uploadFile(List<String> pathList){
        mSubscription = RxManager.getInstance().doSubscribe(new UploadModel().uploadFile(pathList), new RxSubscriber<ResponseBody>(false) {
            @Override
            protected void _onNext(ResponseBody responseBody) {
                try {
                    String result = responseBody.string();
                    LogUtil.e(result, true);
                    mView.onSuccess(new Gson().fromJson(result, Upload.class));
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

            @Override
            protected void _onError() {
                mView.onError();
            }
        });
    }
}
